<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Class	: M_terimabarang
 * 
 * Table	: terimabarang
 *  
 * @author Eko Junaidi Salam
 *
 */
class M_terimabarang extends CI_Model{

	function __construct(){
		parent::__construct();
	}
	
	/**
	 * Fungsi	: getAll
	 * 
	 * Untuk mengambil all-data
	 * 
	 * @param number $start
	 * @param number $page
	 * @param number $limit
	 * @return json
	 */
	function getAll($start, $page, $limit){
		$query  = $this->db->limit($limit, $start)->order_by('ID', 'DESC')->get('terimabarang')->result();
		$total  = $this->db->get('terimabarang')->num_rows();
		
		$data   = array();
		foreach($query as $result){
			$data[] = $result;
		}
		
		$json	= array(
						'success'   => TRUE,
						'message'   => "Loaded data",
						'total'     => $total,
						'data'      => $data
		);
		
		return $json;
	}
	
	/**
	 * Fungsi	: save
	 * 
	 * Untuk menambah data baru atau mengubah data lama
	 * 
	 * @param array $data
	 * @return json
	 */
	function save($data){
		$last   = NULL;
		
		$pkey = array('NOMOR_LPB'=>$data->NOMOR_LPB);
		$cek = $this->db->get_where('do', $pkey);
		if($this->db->get_where('terimabarang', $pkey)->num_rows() > 0){
			/*
			 * Data Exist
			 */
			
			$cek = $cek->result();
			 
			// $tmp = substr($data->SUB_TOTAL,3,strlen($data->SUB_TOTAL));
			// $tmp = str_replace('.','',$tmp);
			// $tmp = str_replace(',','.',$tmp);
			// $data->SUB_TOTAL = $tmp;$tmp = substr($data->PPN,3,strlen($data->PPN));
			// $tmp = str_replace('.','',$tmp);
			// $tmp = str_replace(',','.',$tmp);
			// $data->PPN = $tmp;$tmp = substr($data->TOTAL,3,strlen($data->TOTAL));
			// $tmp = str_replace('.','',$tmp);
			// $tmp = str_replace(',','.',$tmp);
			// $data->TOTAL = $tmp;$tmp = substr($data->MATERAI,3,strlen($data->MATERAI));
			// $tmp = str_replace('.','',$tmp);
			// $tmp = str_replace(',','.',$tmp);
			// $data->MATERAI = $tmp;	
			 
			$arrdatau = array('TANGGAL_LPB'=>(strlen(trim($data->TANGGAL_LPB)) > 0 ? date('Y-m-d', strtotime($data->TANGGAL_LPB)) : NULL),'NOMOR_PO'=>$data->NOMOR_PO,'KODE_SUPPLIER'=>$data->KODE_SUPPLIER,'NAMA_SUPPLIER'=>$data->NAMA_SUPPLIER,'APA_KAPSUL'=>$data->APA_KAPSUL,'JENIS_LPB'=>$data->JENIS_LPB,'NOMOR_INVOICE'=>$data->NOMOR_INVOICE,'TANGGAL_INVOICE'=>(strlen(trim($data->TANGGAL_INVOICE)) > 0 ? date('Y-m-d', strtotime($data->TANGGAL_INVOICE)) : NULL),'NOMOR_PIB'=>$data->NOMOR_PIB,'TANGGAL_PIB'=>(strlen(trim($data->TANGGAL_PIB)) > 0 ? date('Y-m-d', strtotime($data->TANGGAL_PIB)) : NULL),'NOMOR_FAKTUR'=>$data->NOMOR_FAKTUR,'TANGGAL_FAKTUR'=>(strlen(trim($data->TANGGAL_FAKTUR)) > 0 ? date('Y-m-d', strtotime($data->TANGGAL_FAKTUR)) : NULL),'JATUH_TEMPO'=>(strlen(trim($data->JATUH_TEMPO)) > 0 ? date('Y-m-d', strtotime($data->JATUH_TEMPO)) : NULL),'CURRENCY'=>$data->CURRENCY,'KURS'=>$data->KURS,'SUB_TOTAL'=>$data->SUB_TOTAL,'PPN'=>$data->PPN,'TOTAL'=>$data->TOTAL,'MATERAI'=>$data->MATERAI,'DIBUAT_OLEH'=>$data->DIBUAT_OLEH,'JUMLAH_EDIT'=>($cek[0]->JUMLAH_EDIT + 1),'TANGGAL_EDIT'=>(strlen(trim($data->TANGGAL_EDIT)) > 0 ? date('Y-m-d', strtotime($data->TANGGAL_EDIT)) : NULL),'STATUS'=>$data->STATUS);
			 
			$this->db->where($pkey)->update('terimabarang', $arrdatau);
			$last   = $data;
			
		}else{
			/*
			 * Data Not Exist
			 * 
			 * Process Insert
			 */
			// $tmp = substr($data->SUB_TOTAL,3,strlen($data->SUB_TOTAL));
			// $tmp = str_replace('.','',$tmp);
			// $tmp = str_replace(',','.',$tmp);
			// $data->SUB_TOTAL = $tmp;$tmp = substr($data->PPN,3,strlen($data->PPN));
			// $tmp = str_replace('.','',$tmp);
			// $tmp = str_replace(',','.',$tmp);
			// $data->PPN = $tmp;$tmp = substr($data->TOTAL,3,strlen($data->TOTAL));
			// $tmp = str_replace('.','',$tmp);
			// $tmp = str_replace(',','.',$tmp);
			// $data->TOTAL = $tmp;$tmp = substr($data->MATERAI,3,strlen($data->MATERAI));
			// $tmp = str_replace('.','',$tmp);
			// $tmp = str_replace(',','.',$tmp);
			// $data->MATERAI = $tmp;
			
			$jenis = ($data->JENIS_LPB == 'IDR'?($data->APA_KAPSUL == '1'?'capsul':'bbaku'):($data->JENIS_LPB == 'VALAS'?'valas':'import'));
			$kode = $this->m_public_function->GenCode($jenis);
			
			$arrdatac = array('NOMOR_LPB'=>$kode->NOMOR_LPB,'TANGGAL_LPB'=>(strlen(trim($data->TANGGAL_LPB)) > 0 ? date('Y-m-d', strtotime($data->TANGGAL_LPB)) : NULL),'NOMOR_PO'=>$data->NOMOR_PO,'KODE_SUPPLIER'=>$data->KODE_SUPPLIER,'NAMA_SUPPLIER'=>$data->NAMA_SUPPLIER,'APA_KAPSUL'=>$data->APA_KAPSUL,'JENIS_LPB'=>$data->JENIS_LPB,'NOMOR_INVOICE'=>$data->NOMOR_INVOICE,'TANGGAL_INVOICE'=>(strlen(trim($data->TANGGAL_INVOICE)) > 0 ? date('Y-m-d', strtotime($data->TANGGAL_INVOICE)) : NULL),'NOMOR_PIB'=>$data->NOMOR_PIB,'TANGGAL_PIB'=>(strlen(trim($data->TANGGAL_PIB)) > 0 ? date('Y-m-d', strtotime($data->TANGGAL_PIB)) : NULL),'NOMOR_FAKTUR'=>$data->NOMOR_FAKTUR,'TANGGAL_FAKTUR'=>(strlen(trim($data->TANGGAL_FAKTUR)) > 0 ? date('Y-m-d', strtotime($data->TANGGAL_FAKTUR)) : NULL),'JATUH_TEMPO'=>(strlen(trim($data->JATUH_TEMPO)) > 0 ? date('Y-m-d', strtotime($data->JATUH_TEMPO)) : NULL),'CURRENCY'=>$data->CURRENCY,'KURS'=>$data->KURS,'SUB_TOTAL'=>$data->SUB_TOTAL,'PPN'=>$data->PPN,'TOTAL'=>$data->TOTAL,'MATERAI'=>$data->MATERAI,'DIBUAT_OLEH'=>$data->DIBUAT_OLEH,'JUMLAH_EDIT'=>$data->JUMLAH_EDIT,'TANGGAL_EDIT'=>(strlen(trim($data->TANGGAL_EDIT)) > 0 ? date('Y-m-d', strtotime($data->TANGGAL_EDIT)) : NULL),'STATUS'=>$data->STATUS,'ID'=>$data->ID);
			 
			$this->db->insert('terimabarang', $arrdatac);
			$last   = $this->db->where($pkey)->get('terimabarang')->row();
			
			$rs = $this->db->where(array('param' => ($jenis == 'valas'?'LPBV':($jenis == 'capsul'?'LPBC':($jenis == 'import'?'LPBI':'LPBNC')))))->get('init')->result();
			$no = intval(trim($rs[0]->value))+1;
			$this->db->where(array('param' => ($jenis == 'valas'?'LPBV':($jenis == 'capsul'?'LPBC':($jenis == 'import'?'LPBI':'LPBNC')))))->update('init', array('value' => $no));
		}
		
		$total  = $this->db->get('terimabarang')->num_rows();
		
		$json   = array(
						"success"   => TRUE,
						"message"   => 'Data berhasil disimpan',
						'total'     => $total,
						"data"      => $last
		);
		
		return $json;
	}
	
	/**
	 * Fungsi	: delete
	 * 
	 * Untuk menghapus satu data
	 * 
	 * @param array $data
	 * @return json
	 */
	function delete($data){
		$pkey = array('NOMOR_LPB'=>$data->NOMOR_LPB);
		
		$this->db->where($pkey)->delete('terimabarang');
		
		$total  = $this->db->get('terimabarang')->num_rows();
		$last = $this->db->get('terimabarang')->result();
		
		$json   = array(
						"success"   => TRUE,
						"message"   => 'Data berhasil dihapus',
						'total'     => $total,
						"data"      => $last
		);				
		return $json;
	}
}
?>